#include "BlockQueue.hpp"
#include "Thread.hpp"
#include "Task.hpp"
#include <vector>
#include <ctime>
using namespace std;
using namespace ThreadModule;

int a = 10;

using blockqueue_t = BlockQueue<Task>;
class ThreadData
{
public:
private:
    blockqueue_t &bq;
    string who;
};
void Consumer(blockqueue_t &bq)
{
    while (1)
    {
        Task t;
        bq.Pop(&t);    
        t.Excute();
        cout << "Consumer Consum data is:  " << t.ResultToString() << endl;
    }
}
void Productor(blockqueue_t &bq)
{
    srand(time(nullptr) ^ pthread_self());
    while (1)
    {
        sleep(1);
        int a = rand() % 10 + 1;
        usleep(1234);
        int b = rand() % 20 + 1;   
        Task t(a, b);   
        bq.Enqueue(t);    
        cout << "Productor product data is: " << t.DebugToString() << endl;
    }
}

void StartComm(vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq, func_t<blockqueue_t> func)
{
    for (int i = 0; i < num; i++)
    {
        string name = "thread" + to_string(i + 1);
        threads->emplace_back(func, bq, name);
        threads->back().Start();
    }
}
void StartConsumer(vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq)
{

    StartComm(threads, num, bq, Consumer);
}

void StartProduct(vector<Thread<blockqueue_t>> *threads, int num, blockqueue_t &bq)
{
    StartComm(threads, num, bq, Productor);
}

void WaitAllThread(vector<Thread<blockqueue_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}

int main()
{
    blockqueue_t *bq = new blockqueue_t(5);
    vector<Thread<blockqueue_t>> threads;
    StartConsumer(&threads, 1, *bq);
    StartProduct(&threads, 1, *bq);
    WaitAllThread(threads);
}